package com.flowersinc.shop.views
{
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.MapType;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.containers.Box;
	
	import org.goverla.managers.ApplicationManager;

	public class ContactsViewBase extends Box	
	{
		public var map : Map;
		
		[Bindable]
		protected var aboutXML : XML;
		
		[Bindable(event="aboutXMLChanged")]
		protected function get email() : String
		{
			return "<a href='mailto:" + aboutXML.@email + "'><u>" + aboutXML.@email + "</u></a>";
		}
				
		protected function onInitialize() : void {
			_urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
			_urlLoader.load(_urlRequest);
		}
		
		protected function onMapReady(event : Event):void {
			updateMapCenter();
		}

		private function onLoadComplete(event : Event) : void { 
			aboutXML = XML(_urlLoader.data);
			updateMapCenter();
			
			dispatchEvent(new Event("aboutXMLChanged"));
		}
		
		private function updateMapCenter() : void {
			if (aboutXML != null && map != null && map.isLoaded()) {
				map.setCenter(new LatLng(Number(aboutXML.map.@lat), Number(aboutXML.map.@lng)), 14, 
					MapType.NORMAL_MAP_TYPE);
			}
		}
		
		private var _urlRequest : URLRequest = 
			new URLRequest(ApplicationManager.instance.url + "data/about.xml"); 
		private var _urlLoader : URLLoader = new URLLoader();		
	}
}